package leetcode.code1124;

import java.util.Map;
import java.util.TreeMap;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int longestWPI(int[] hours) {
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		tm.put(0, -1);
		int ans = 0;
		for (int i = 0, t = 0, nt = 0, min = 0; i < hours.length; i++) {
			int h = hours[i];
			if (h > 8) {
				t++;
			} else {
				nt++;
			}
			int tnt = t - nt;
			Map.Entry<Integer, Integer> L = tm.floorEntry(tnt - 1);
			if (L != null) {
				ans = Math.max(ans, i - L.getValue());
			}
			if (tnt < min) {
				tm.put(tnt, i);
				min = tnt;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(3, this.longestWPI(H.str2array3("[9,9,9]")));
		H.compare(3, this.longestWPI(H.str2array3("[9,6,9]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
